import { getSlideVideoBack, getNewsList, getHostMatchList, getFootballLiveList, getBasketballLiveList } from '@/server/news'
import { getStartEndTime } from '@/common/tool'
import { getHotTeamData } from '@/server/news'
import { NavList } from '@/common/enum'
export const getLiveInfo = (matchType) => {
  return new Promise(async (resolve, reject) => {
    const matchApi = {
      [process.env.NEXT_PUBLIC_Basketball]: getBasketballLiveList,
      [process.env.NEXT_PUBLIC_Football]: getFootballLiveList
    }
    //[获取精彩视频,获取积分榜联赛列表,首页视频锦集,首页录像回放锦集,热门联赛列表,新闻模块数据]
    const [smileVideoData, newsListData, hotMatch, todayLiveData, yesterdayLiveData] = await Promise.all([getSlideVideoBack({ num: 10, type: 3, newsTypeid: matchType }), getNewsList(matchType, 1, 10), getHostMatchList(11, 1),
    Reflect.get(matchApi, matchType)?.({ pageNum: 1, pageSize: 1000, type: 1, date: getStartEndTime().todayStartTime }),
    Reflect.get(matchApi, matchType)?.({ pageNum: 1, pageSize: 1000, type: 1, date: getStartEndTime().tomorrowEndTime })]
    )

    const smileVideo = { otherData: [], swiperData: [] }
    //处理精彩视频数据
    smileVideo.swiperData = smileVideoData.filter(row => row?.coverImg !== '').slice(0, 3)
    smileVideoData.forEach(row => {
      const index = smileVideo.swiperData.findIndex(r => r.id === row.id)
      if (index === -1) smileVideo.otherData.push(row)
    })

    // //处理新闻数据
    const newsList = { otherData: [], swiperData: [] }
    newsList.swiperData = newsListData?.records?.filter(row => row?.coverImg !== '').slice(0, 3) || []
    newsListData?.records?.forEach(row => {
      const index = newsList.swiperData.findIndex(r => r.id === row.id)
      if (index === -1) newsList.otherData.push(row)
    })

    resolve({
      smileVideo,
      newsList,
      matchType,
      hotMatch,
      liveListData: {
        today: {
          time: getStartEndTime().todayStartTime,
          data: todayLiveData
        },
        yesterday: {
          time: getStartEndTime().tomorrowEndTime,
          data: yesterdayLiveData
        }
      }
    })
  })
}



export const getLiveInfoForId = (matchType, context) => {
  return new Promise(async resolve => {
    const list = {
      [process.env.NEXT_PUBLIC_Basketball]: NavList?.b,
      [process.env.NEXT_PUBLIC_Football]: NavList?.f
    }
    const matchApi = {
      [process.env.NEXT_PUBLIC_Basketball]: getBasketballLiveList,
      [process.env.NEXT_PUBLIC_Football]: getFootballLiveList
    }
    const checkNumber = (value) => !isNaN(parseFloat(value)) && isFinite(value)
    let eventId = Reflect.get(list, matchType)?.find(r => r?.en === context.params.id[0])?.id
    if (context.params.id[1] && checkNumber(Number(context.params.id[1]))) {
      eventId = Number(context.params.id[1])
    }
    const [hotTeamData, newsListData, smileVideoData, todayLiveData, yesterdayLiveData] = await Promise.all([getHotTeamData({ eventId, num: 10 }), getNewsList(eventId, 1, 10), getSlideVideoBack({ num: 10, type: 3, typeGeneral: matchType }),
    Reflect.get(matchApi, matchType)?.({ pageNum: 1, pageSize: 1000, type: 1, date: getStartEndTime().todayStartTime, eventIds: eventId }),
    Reflect.get(matchApi, matchType)?.({ pageNum: 1, pageSize: 1000, type: 1, date: getStartEndTime().tomorrowEndTime, eventIds: eventId })])

    const smileVideo = { otherData: [], swiperData: [] }
    //处理精彩视频数据
    smileVideo.swiperData = smileVideoData.filter(row => row?.coverImg !== '').slice(0, 3)
    smileVideoData.forEach(row => {
      const index = smileVideo.swiperData.findIndex(r => r.id === row.id)
      if (index === -1) smileVideo.otherData.push(row)
    })

    // //处理新闻数据
    const newsList = { otherData: [], swiperData: [] }
    newsList.swiperData = newsListData?.records?.filter(row => row?.coverImg !== '').slice(0, 3) || []
    newsListData?.records?.forEach(row => {
      const index = newsList.swiperData.findIndex(r => r.id === row.id)
      if (index === -1) newsList.otherData.push(row)
    })
    const liveListData = {};
    if (todayLiveData?.length > 0) {
      liveListData.today = {
        time: getStartEndTime().todayStartTime,
        data: todayLiveData
      }
    }
    if (yesterdayLiveData?.length > 0) {
      liveListData.yesterday = {
        time: getStartEndTime().tomorrowEndTime,
        data: yesterdayLiveData
      }
    }
    resolve({
      eventId,
      hotTeamData,
      newsList,
      smileVideo,
      matchType,
      liveListData,
    })
  })
}